package com.andrew.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.andrew.entity.Customer;
import com.andrew.entity.Order;
import com.andrew.service.OrderService;
import com.andrew.service.impl.OrderServiceImpl;
import com.google.gson.Gson;

public class GetMyOrdersServlet extends HttpServlet {
	private OrderService orderService=new OrderServiceImpl();
	private Gson gson=new Gson();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置编码
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
				
		//获取当前用户
		Customer customer=(Customer)req.getSession().getAttribute("customer");
		try {
			//获取当前用户的订单详情
			List<Order> myOrders = orderService.getMyOrders(customer);
			//转换为json输出
			String json=gson.toJson(myOrders).toString();
			resp.getWriter().write(json);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}
}
